﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Collections.Generic;

namespace GoShopping.Web.SystemManager
{
    public partial class LockedUsers : System.Web.UI.Page
    {
        BLL.UserBean ub = new GoShopping.BLL.UserBean();
        BLL.MemberShipService ms = new GoShopping.BLL.MemberShipService();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //Master.PrdtButton.Visible = false;
                //Master.SupplierButton.Visible = false;
                //Master.ElementSelectedChange = GetTreeView;
                GetGVData(null);
            }
        }
        private void GetTreeView()
        {
            TreeView tv = Master.MasterTreeView;
            SetControlCommon.GetPrdtTreeView(tv, Server.MapPath("~/ProductManager/Navigation.xml"), "sys");
        }
        protected void btnUnlock_Click(object sender, EventArgs e)
        {
            LockOrUnLockUser(true);
            GetGVData(null);
        }

        private void LockOrUnLockUser(bool isLock)
        {
            // 使用ArrayList记录选择的员工编号
            ArrayList selList = new ArrayList();
            int i = 0;
            // 遍历数据集
            foreach (GridViewRow row in this.extGVUser.Rows)
            {
                // 获取行首的CheckBox，判断是否为Checked
                CheckBox cb = (CheckBox)row.FindControl("cbSingle");
                if (cb.Checked == false)
                {
                    continue;
                }
                //// 若为Checked，将信息添加到ArrayList
                selList.Add(((Label)row.FindControl("lblName")).Text);
                i++;
            }
            // 若遍历完成后ArrayList为空，则表示未选中任何项，页面提示
            if (selList.Count == 0)
            {
                return;
            }
            //执行解锁操作
            for (int j = 0; j < selList.Count; j++)
            {
                ms.ApproveMember(selList[j].ToString(), isLock);
            }
            // 重新绑定员工列表
            GetGVData(null);
        }
        private void GetGVData(string filterString)
        {
            IList<MembershipUser> iUser = ms.GetAllMembers();
            for (int i = 0; i < iUser.Count; i++)
            {
                if (iUser[i].IsApproved == true)
                {
                    iUser.RemoveAt(i);
                    i--;
                }
            }
            this.extGVUser.DataSource = iUser;
            this.extGVUser.DataBind();
        }
        //private void GetGVData(string filterString)
        //{
        //    if (string.IsNullOrEmpty(filterString))
        //    {
        //        IList<Model.UserInfo> iUser = ub.GetAllUserList();
        //        this.extGVUser.DataSource = iUser;
        //        this.extGVUser.DataBind();
        //    }
        //    else
        //    {
        //        IList<Model.UserInfo> iUser = ub.GetUserList(filterString);
        //        this.extGVUser.DataSource = iUser;
        //        this.extGVUser.DataBind();
        //    }

        //}
        protected void extGVUser_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.extGVUser.PageIndex = e.NewPageIndex;
            GetGVData(null);
        }

        protected void extGVUser_RowCommand(object sender, GridViewCommandEventArgs e)
        {

        }

        protected void cbAll_CheckedChanged(object sender, EventArgs e)
        {
            // 遍历数据集的所有数据项
            foreach (GridViewRow row in this.extGVUser.Rows)
            {
                // 取得数据项行首的CheckBox，使其Checked属性与表头的CheckBox同步
                CheckBox subCB = (CheckBox)row.FindControl("cbSingle");
                subCB.Checked = ((CheckBox)sender).Checked;
            }
        }
        public string ChangeToChinese(object obj)
        {
            if (Convert.ToBoolean(obj))
            {
                return "是";
            }
            else
            {
                return "否";
            }
        }
    }
}
